import common from "./common";
import constants from "./constants";

const fileApi = {
    url: {
        uploadFile: window.configs.requestPrefix + constants.FILE_UploadFile,
        getFile: window.configs.requestPrefix + constants.File_GetFile,
        getVideo: window.configs.requestPrefix + constants.File_GetVideo,
        getFileWithType: window.configs.requestPrefix + constants.File_GetFileWithType,
    },
    uploadFile: (data, success, error) => {
        return common.requestData(constants.FILE_UploadFile, data, success, error);
    },
    uploadFormData: (formData, success, error,fail) => {
        return common.uploadFormData(constants.FILE_UploadFile, formData, success, error,fail);
    },
    /**
     * 通过FormData方式切片上传文件，
     * @param formData
     * @param success
     * @param error
     * @returns {*}
     */
    uploadFormFileSlice: (formData, success, error) => {
        return common.uploadFormData(constants.FILE_UploadFormFileSlice, formData, success, error);
    },
    importFormData: (url, formData, success, error) => {
        return common.uploadFormData(url, formData, success, error);
    },
    exportData: (url, data, config, success) => {
        return common.exportData(url, data, config, success);
    },
    deleteFileInfo4Unused: (data, success, error) => {
        return common.requestData(constants.FILE_DeleteFileInfo4Unused, data, success, error);
    },
    findFileInfos4Unused: (data, success, error) => {
        return common.requestData(constants.FILE_FindFileInfos4Unused, data, success, error);
    },
    getFile: (sid, success, error) => {
        return common.getData(constants.File_GetFile + sid, success, error);
    },
    getFileContent: (sid, success, error) => {
        return common.getDataText(constants.File_GetFile + sid, success, error);
    },
    getFileFromUrl: (url, success, error) => {
        return common.getData(url, success, error);
    },
    getTextFileFromUrl: (url, success, error) => {
        return common.getDataText(url, success, error);
    },
    getFileWithVer: (data, success, error) => {
        return common.getData(constants.File_GetFile + data.sid + '?ver=' + data.ver, success, error);
    },
    getFileContentWithVer: (data, success, error) => {
        return common.getDataText(constants.File_GetFile + data.sid + '?ver=' + data.ver, success, error);
    },
    getFileInfo: (sid, success, error) => {
        return common.requestData(constants.File_GetFileInfo, {sid, showToast: false}, success, error);
    },
    browserDownloadFile: (data, config, success, error) => {
        return common.exportData(constants.File_BrowserDownloadFile, data, config, success, error);
    },
    mergePdfFiles: (data, success, error) =>{
        return common.requestData(constants.File_MergePdfFiles, data, success, error);
    },
    findCommonFiles: (data, success, error) =>{
        return common.requestData(constants.File_FindCommonFiles, data, success, error);
    },
    //ajax下载的文件通过此方法进行下载
    downloadFile (arraybuffer, fileName,t) {
        //默认type对应excel
        let type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
        if (t === "pdf"){
            type= "application/pdf;charset=utf-8";
        }
        let blob = new Blob([arraybuffer], {
            type
        });
        fileApi.downloadBlobFile(blob, fileName);
    },
    downloadBlobFile (blob, fileName) {
        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveBlob(blob, fileName);
        } else {
            let link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = fileName;
            link.click();
            window.URL.revokeObjectURL(link.href);
        }
    },
};

export default fileApi;
